package com.example.optimizaapp;

import java.util.ArrayList;
import java.util.Locale;

import com.example.optimizaapp.domain.Cliente;
import com.example.optimizaapp.domain.ManejadorCliente;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class BuscarEmpresa extends Activity {
	ManejadorCliente mc= ManejadorCliente.getManejadorCliente();
	private ListView mainListView;
	private ArrayAdapter<String> listAdapter ;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_buscar_empresa);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    // TODO Auto-generated method stub
		 this.mainListView=(ListView) findViewById(R.id.listaClientes);
	    
	}

	public void pressedBuscarEmpresa(View view) {
		EditText nEmpresa = (EditText) findViewById(R.id.txtBuscarEmpresa);
		if (!nEmpresa.getText().toString().equals("")){
			String nomEmpresa =nEmpresa.getText().toString();
			InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(nEmpresa.getWindowToken(), 0);
			final ArrayList<Cliente> clientes = this.buscarEmpresa(nomEmpresa);
			if (clientes.size() > 0){
				ArrayList <String> nomClientes = new ArrayList<String>();
				for (int i = 0; i<clientes.size(); i++){
					Cliente c = clientes.get(i);
					nomClientes.add(c.getNombreCliente());
				}
				
				listAdapter= new ArrayAdapter<String>(this, R.layout.simple_row, nomClientes);
			    this.mainListView.setAdapter(listAdapter);
			    this.mainListView.setOnItemClickListener(new OnItemClickListener() {
			    	public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
			    		Cliente c =(Cliente) clientes.get(position);
			    		Intent intent = new Intent(getBaseContext(), DetalleEmpresa.class);
			        	intent.putExtra("clientName", c.getNombreCliente());
			        	startActivity(intent);
			    	}
			    });
				
			}else{
				new AlertDialog.Builder(this).setTitle("Error").setMessage("No se encontro empresa").setNeutralButton("OK", null).show();
			}
		}else{
			new AlertDialog.Builder(this).setTitle("Error").setMessage("Para comenzar la busqueda, debe ingresar al menos un caracter").setNeutralButton("OK", null).show();
		}
	}
	
	
	public ArrayList<Cliente> buscarEmpresa(String nom){
		// using the default system Locale
		
		ArrayList<Cliente>clientesEncontrados = new ArrayList<Cliente>();
		
	    Locale defloc = Locale.getDefault();
		for (int i = 0 ; i<mc.getClientes().size(); i++){
			Cliente cli = (Cliente)mc.getClientes().get(i);
			
			if (cli.getNombreCliente().toLowerCase(defloc).contains(nom.toLowerCase(defloc))){
				clientesEncontrados.add(cli);
			}
		}
		
		return clientesEncontrados;
	}
	
}
